
/**
 * @FileName UserController.java
 * @Author 史旭升
 * @Date 2022-12-27  9:35
 * @Description 该文件为具体的用户管理界面的对应的控制器类
 */
package com.sd365.permission.centre.api.controller;

import com.sd365.common.core.common.controller.AbstractController;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.api.UserApi;
import com.sd365.permission.centre.entity.Department;
import com.sd365.permission.centre.entity.Position;
import com.sd365.permission.centre.entity.Role;
import com.sd365.permission.centre.entity.User;
import com.sd365.permission.centre.pojo.dto.UserDTO;
import com.sd365.permission.centre.pojo.query.UserQuery;
import com.sd365.permission.centre.pojo.vo.ResourceVO;
import com.sd365.permission.centre.pojo.vo.UserVO;
import com.sd365.permission.centre.service.LoginService;
import com.sd365.permission.centre.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName UserController
 * @Description
 * @Author yangshaoqi
 * @Date 2020/12/14 21:11
 * @Version 1.0
 **/
@RestController
public class UserController extends AbstractController implements UserApi {

    /**
     *用户服务
     */
    @Autowired
    private UserService userSerice;
    /**
     *  登录服务
     */
    @Autowired
    LoginService loginService;

    /**
     * @Description: 用户权限列表
     * @Author: 史旭升
     * @DATE: 2022-12-28  21:01
     * @param: 用户id
     * @return: 用户所对应的角色的权限列表
     */
    @ApiLog
    @Override
    public List<ResourceVO> getUserResourceVO(Long id) {
        List<Role> roles = loginService.getRolesByUserId(id);
        return  loginService.getResourceVO(roles);
    }

    /**
     * @Description: 用户管理界面增加页面保存数据调用此接口
     * 增加用户
     * @Author: 史旭升
     * @DATE: 2022-12-27  10:12
     * @param: userDTO
     * @return: Boolean
     */
    @ApiLog
    @Override
    public Boolean add(@RequestBody  @Valid UserDTO userDTO) {
        return userSerice.add(userDTO);
    }

    /**
     * @param: 删除用户，若有对应角色则中间表用户-角色也需一并删除
     * @return: 成功则true
     * @DATE: 2022-12-27  11:15
     * @param: Long id, Long version
     */
    @ApiLog
    @Override
    public Boolean remove(Long id, Long version) {
        return userSerice.remove(id, version);
    }

    /**
     *修改用户
     * @author shi xusheng
     * @date 2022/12/27 11:45
     * @param userDTO
     * @return java.lang.Boolean
     */
    @ApiLog
    @Override
    public Boolean modify(@Valid UserDTO userDTO) {
        return userSerice.modify(userDTO);
    }

    /**
     *修改用户角色
     * @author shi xusheng
     * @date 2022/12/28 12:36
     * @param userDTOS
     * @return java.lang.Boolean
     */
    @ApiLog
    @Override
    public Boolean modifyWithNewRole(@Valid UserDTO[] userDTOS) {
        return userSerice.modifyWithNewRole(userDTOS);
    }

    /**
     *查询用户
     * @author shi xusheng
     * @date 2022/12/30 2:37
     * @param userQuery
     * @return java.util.List<com.sd365.permission.centre.entity.User>
     */
    @ApiLog
    @Override
    public List<User> commonQuery(UserQuery userQuery) {
        return userSerice.commonQuery(userQuery);
    }

    /**
     *查询用户通过id
     * @author shi xusheng
     * @date 2022/12/30 22:38
     * @param id
     * @return com.sd365.permission.centre.pojo.vo.UserVO
     */
    @ApiLog
    @Override
    public UserVO queryUserById(Long id) {
        User user = userSerice.queryById(id);
        return BeanUtil.copy(user, UserVO.class);

    }

    /**
     *查询所有角色
     * @author shi xusheng
     * @date 2022/12/28 21:39
     * @return java.util.List<com.sd365.permission.centre.entity.Role>
     */
    @ApiLog
    @Override
    public List<Role> queryAllRole() {
        return userSerice.queryAllRole();
    }

    /**
     *查询所有部门
     * @author shi xusheng
     * @date 2022/12/28 22:39
     * @return java.util.List<com.sd365.permission.centre.entity.Department>
     */
    @ApiLog
    @Override
    public List<Department> queryAllDepartment() {
        return userSerice.queryAllDepartment();
    }

    /**
     *查询所有职业
     * @author shi xusheng
     * @date 2022/12/30 22:40
     * @return java.util.List<com.sd365.permission.centre.entity.Position>
     */
    @ApiLog
    @Override
    public List<Position> queryAllPosition() {
        return userSerice.queryAllPosition();
    }

    /**
     *批量修改用户(启停控制)
     * @author shi xusheng
     * @date 2022/12/30 22:41
     * @param userDTOS
     * @return java.lang.Boolean
     */
    @ApiLog
    @Override
    public Boolean batchUpdate(@Valid UserDTO[] userDTOS) {
        return userSerice.batchUpdate(userDTOS);
    }

    /**
     *批量删除
     * @author shi xusheng
     * @date 2022/12/28 15:42
     * @param userDTOS
     * @return java.lang.Boolean
     */
    @ApiLog
    @Override
    public Boolean batchDelete(@Valid UserDTO[] userDTOS) {
        return userSerice.batchDelete(userDTOS);
    }

    /**
     * md5
     * @return boolean
     */
    @ApiLog
    @Override
    public Boolean firstStartMd5() {
        return true;
    }

    /**
     *重新更新密码，本次培训项目不需要实现这个
     * @author shi xusheng
     * @date 2022/12/30 22:45
     * @param userDTO
     * @return java.lang.Boolean
     */
    @ApiLog
    @Override
    public Boolean updatePassword(@Valid UserDTO userDTO) {
        return userSerice.updatePassword(userDTO);
    }

    /**
     *找回密码
     * @author shi xusheng
     * @date 2022/12/30 22:49
     * @param code
     * @return com.sd365.permission.centre.entity.User
     */
    @ApiLog
    @Override
    public User getUser(String code) {
        return userSerice.getUser(code);
    }
}

